package com.woniu113.junk.site.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.util.AliyunOSS;
import com.woniu113.junk.site.exception.WasteTypeException;
import com.woniu113.junk.site.mapper.WastePriceMapper;
import com.woniu113.junk.site.model.WastePrice;
import com.woniu113.junk.site.model.WasteType;
import com.woniu113.junk.site.mapper.WasteTypeMapper;
import com.woniu113.junk.site.param.wastetype.AddWasteTypeParam;
import com.woniu113.junk.site.param.wastetype.AllWasteTypeParam;
import com.woniu113.junk.site.service.WasteTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class WasteTypeServiceImpl extends ServiceImpl<WasteTypeMapper, WasteType> implements WasteTypeService {
    @Resource
    private AliyunOSS aliyunOSS;
    @Resource
    private WasteTypeMapper wasteTypeMapper;
    @Resource
    private WastePriceMapper wastePriceMapper;


    @Override
    public WasteType addWasteType(AddWasteTypeParam param) throws Exception{
        //1.判断新增数据完整性
        if (param.getTypeName()==null || param.getIcon()==null || param.getIntro()==null){
            throw new WasteTypeException("请将数据填写完整",2001);
        }
        //2.判断是否已存在该类型名(查询类别名相同,类别状态不为删除的)
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_name",param.getTypeName()).ne("type_state", Data.WASTE_TYPE_STATE_DELETED);
        List<WasteType> wasteTypes = wasteTypeMapper.selectList(queryWrapper);
        if (!wasteTypes.isEmpty()){
            //查询结果不为空,表示有相同类型名存在,此时抛出异常
            throw new WasteTypeException("要新增的类别名已存在",2002);
        }
        //3.处理图片
        String filePath = aliyunOSS.ProcessPicture(param.getIcon());
        //4.录入数据,执行新增操作
        WasteType newWasteType = new WasteType();
        newWasteType.setTypeName(param.getTypeName());
        newWasteType.setIcon(filePath);
        newWasteType.setIntro(param.getIntro());
        newWasteType.setTypeState(Data.WASTE_TYPE_STATE_NORMAL);
        wasteTypeMapper.insert(newWasteType);
        return newWasteType;
    }

    @Override
    public void deleteWasteType(Integer typeId) {
        //1.判断typeId是否为空
        if (typeId==null){
            throw new WasteTypeException("传入的typeId不能为空",2003);
        }
        //2.判断废品类型id是否存在废品类型数据
        WasteType wasteType = wasteTypeMapper.selectById(typeId);
        if (wasteType==null){//查询结果为空,表示该类型id不存在类型数据
            throw new WasteTypeException("不存在该类型数据",2004);
        }
        //3.判断废品类型是否为已删除,已删除则不能再删除
        if (wasteType.getTypeState()==Data.WASTE_TYPE_STATE_DELETED){
            throw new WasteTypeException("废品类型已被删除,无法再进行删除",2005);
        }
        //4.执行删除操作(修改状态为删除)
        wasteType.setTypeState(Data.WASTE_TYPE_STATE_DELETED);
        wasteTypeMapper.updateById(wasteType);
    }

    @Override
    public void disableWasteType(Integer typeId) {
        //1.判断typeId是否为空
        if (typeId==null){
            throw new WasteTypeException("传入的typeId不能为空",2003);
        }
        //2.判断废品类型id是否存在废品类型数据
        WasteType wasteType = wasteTypeMapper.selectById(typeId);
        if (wasteType==null){//查询结果为空,表示该类型id不存在类型数据
            throw new WasteTypeException("不存在该类型数据",2004);
        }
        //3.判断废品类型是否为已删除或者禁用,已删除或者禁用则不能再禁用(只有正常状态下才能禁用)
        if (wasteType.getTypeState()!=Data.WASTE_TYPE_STATE_NORMAL){
            throw new WasteTypeException("废品状态异常,无法禁用",2006);
        }
        //4.执行禁用操作(修改状态为禁用)
        wasteType.setTypeState(Data.WASTE_TYPE_STATE_DISABLED);
        wasteTypeMapper.updateById(wasteType);
    }

    @Override
    public void ableWasteType(Integer typeId) {
        //1.判断typeId是否为空
        if (typeId==null){
            throw new WasteTypeException("传入的typeId不能为空",2003);
        }
        //2.判断废品类型id是否存在废品类型数据
        WasteType wasteType = wasteTypeMapper.selectById(typeId);
        if (wasteType==null){//查询结果为空,表示该类型id不存在类型数据
            throw new WasteTypeException("不存在该类型数据",2004);
        }
        //3.判断废品类型是否为禁用,不为禁用则不能解禁
        if (wasteType.getTypeState()!=Data.WASTE_TYPE_STATE_DISABLED){
            throw new WasteTypeException("废品状态异常,无法解禁",2007);
        }
        //4.执行解禁操作(修改状态为正常)
        wasteType.setTypeState(Data.WASTE_TYPE_STATE_NORMAL);
        wasteTypeMapper.updateById(wasteType);
    }

    @Override
    public Page<WasteType> findAll(AllWasteTypeParam param) {
        Page<WasteType> page = new Page<>(param.getPageNum(), param.getPageSize());// 开启分页
        List<WasteType> wasteTypeList = wasteTypeMapper.findAll(param.getTypeName(),page);
        page.setRecords(wasteTypeList);
        return page;
    }

    @Override
    public List<WasteType> findWasteTypeNoWastePrice(Integer stationId) {
        //1.判断数据完整性
        if (stationId==null){
            throw new WasteTypeException("请将数据填写完整",2001);
        }
        //2.在废品价格表中查询该站点的所有站点价格信息
        QueryWrapper<WastePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",stationId);
        List<WastePrice> wastePrices = wastePriceMapper.selectList(queryWrapper);
        //3.获取所有废品类型
        List<WasteType> wasteTypeList = wasteTypeMapper.all();
        //4.如果废品价格为空，返回所有废品类型
        if (wastePrices.isEmpty()) {//为空,表示该区站点所有废品类型均未设置价格,返回所有废品类型
            return wasteTypeList;
        }
        //5.提取已有价格的废品类型ID
        Set<Integer> wasteTypeIds = wastePrices.stream().map(wastePrice->wastePrice.getTypeId()).collect(Collectors.toSet());
        //6.剔除已有价格的废品类型
        List<WasteType> wasteTypes = wasteTypeList.stream().filter(wasteType -> !wasteTypeIds.contains(wasteType.getTypeId())).collect(Collectors.toList());
        //7.返回剔除后的废品类型信息
        return wasteTypes;
    }
}
